<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>粉丝列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
	<style>
		.layui-col-md3 button{margin-bottom:10px;}
	</style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card layui-col-md12">
          <div class="layui-card-header"><i class="fa fa-list"></i> 粉丝列表</div>
          <div class="layui-card-body" pad15>
						<div class="layui-col-md3">
							<button class="layui-btn layui-btn-primary" onclick="tongbu()"><i class="fa fa-refresh"></i>从微信公众号中同步</button>
							<button class="layui-btn layui-btn-primary" onclick="openmax('{:url('tagslist')}/isopen/1')">标签管理</button>
							<button class="layui-btn layui-btn-primary" onclick="settag(0)">设置标签</button>
							<button class="layui-btn layui-btn-primary" onclick="datadel(0)">删除</button>
						</div>
						<div class="layui-form layui-col-md9 layui-form-search">
							<div class="layui-inline">
								<label class="layui-form-label">标签</label>
								<div class="layui-input-inline">
									<select name="tagid">
										<option value="">全部</option>
										{foreach $tagslist as $tag}
										<option value="{$tag.tagid}" {if input('param.tagid')==$tag['tagid']}selected{/if}>{$tag.name}</option>
										{/foreach}
									</select>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">状态</label>
								<div class="layui-input-inline">
									<select name="subscribe">
										<option value="">全部</option>
										<option value="1">已关注</option>
										<option value="0">已取消</option>
									</select>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">性别</label>
								<div class="layui-input-inline">
									<select name="sex">
										<option value="">全部</option>
										<option value="男">男</option>
										<option value="女">女</option>
										<option value="未知">未知</option>
									</select>
								</div>
							</div>
							<div class="layui-inline layuiadmin-input-useradmin">
								<label class="layui-form-label">昵称</label>
								<div class="layui-input-inline">
									<input type="text" name="nickname" autocomplete="off" class="layui-input" value="">
								</div>
							</div>
							<div class="layui-inline layuiadmin-input-useradmin">
								<label class="layui-form-label">关注时间</label>
								<div class="layui-input-inline" style="width:200px">
									<input type="text" id="ctime" name="subscribe_time" autocomplete="off" class="layui-input" value="">
								</div>
							</div>
							<div class="layui-inline">
								<button class="layui-btn layuiadmin-btn-replys" lay-submit="" lay-filter="LAY-app-forumreply-search">
									<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
								</button>
							</div>
						</div>
						<div class="layui-col-md12">
							<table id="tabledata" lay-filter="tabledata"></table>
						</div>
          </div>
        </div>
    </div>
  </div>
	{include file="public/js"/}
	<script>
	layui.laydate.render({ 
		elem: '#ctime',
		trigger: 'click',
		range: '~' //或 range: '~' 来自定义分割字符
	});
  var table = layui.table;
	var datawhere = {};
  //数据表
  var tableIns = table.render({
    elem: '#tabledata'
    ,url: '{$Request.url}' //数据接口
    ,page: true //开启分页
    ,cols: [[ //表头
			{type:"checkbox"},
      //{field: 'id', title: 'ID',sort:true, width:80},
      {field: 'openid', title: 'openid', width:280},
      {field: 'nickname', title: '头像昵称',templet: '<div><img src="{{d.headimgurl}}" style="height:50px"/> {{d.nickname}}</div>', width:280},
      {field: 'sex', title: '性别',width:100},
      {field: 'area', title: '地区',templet: function(d){ 
				if(d.country!='中国'){
					return d.country
				}else{
					return (d.province!==null?d.province:'') + (d.city!==null?d.city:'')
				}
			}},
			{field: 'tagname', title: '标签'},
      {field: 'subscribe', title: '状态',templet: function(d){ 
				if(d.subscribe == 1){
					return '已关注'
				}else if(d.subscribe == 0){
					return '未关注'
				}
			}},
      {field: 'subscribe_time', title: '关注时间',templet:function(d){ return date('Y-m-d H:i',d.subscribe_time)}},
      {field: 'operation', title: '操作',templet:'<div><button class="table-btn" onclick="settag({{d.id}})">设置标签</button><button class="table-btn" onclick="datadel({{d.id}})">删除</button></div>'}
    ]]
  });
	//排序
	table.on('sort(tabledata)', function(obj){
		datawhere.field = obj.field;
		datawhere.order = obj.type;
		tableIns.reload({
			initSort: obj,
			where: datawhere
		});
	});
	//检索
	layui.form.on('submit(LAY-app-forumreply-search)', function(obj){
		var field = obj.field
		var olddatawhere = datawhere
		datawhere = field
		datawhere.field = olddatawhere.field
		datawhere.order = olddatawhere.order
		tableIns.reload({
			where: datawhere,
			page: {curr: 1}
		});
	})
	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要删除吗？删除后无法恢复！',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('fansdel')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	var infonum = 0;
	var next_openid = '';
	var totalpage = 0;
	var getopenidnum = 0;
	var openids = [];
	var countopenid = 0;
	var totalfans = 0;
	var loadingdialog;
	function tongbu(){
		layer.confirm('如果粉丝量较大,同步时间会比较长,请耐心等待,确定要同步吗?',{icon: 7, title:'从微信公众号同步粉丝数据'}, function(index){
			layer.close(index)
			infonum = 0;
			next_openid = '';
			totalpage = 0;
			getopenidnum = 0;
			openids = [];
			countopenid = 0;
			loadingdialog = layer.open({type:1,content:'<div style="padding:10px 5px"><div class="flex-y-center"><img src="__STATIC__/admin/img/loading.gif"/><span style="font-size:20px;padding-left:5px">正在同步请勿操作...</span></div></div>',area:'300px',title:false,closeBtn:false});
			$.post("{:url('tbfans')}",{ op:'getpage'},function(data){
				if(data.status==0){
					layer.close(loadingdialog);
					dialog(data.msg,data.status);
				}else{
					totalpage = data.totalpage;
					totalfans = data.total;
					layer.msg('共计'+data.total+'个粉丝',{offset:'100px'});
					getopenids();
				}
			},'json');
		});
	}
	function getuserinfo(){
		layer.msg('正在获取第'+(getopenidnum*10000+infonum*100+1)+'~'+((getopenidnum*10000+(infonum+1)*100) < totalfans ? (getopenidnum*10000+(infonum+1)*100) : totalfans)+'条用户信息',{offset:'100px'});
		$.post("{:url('tbfans')}",{ op:'getuserinfo',openids:openids[infonum]},function(data){
			infonum++;
			if(infonum < openids.length){
				getuserinfo();
			}else{
				getopenidnum++;
				if(getopenidnum < totalpage){
					infonum = 0
					getopenids();
				}else{
					layer.msg('正在做最后处理...',{offset:'100px'});
					$.post("{:url('tbfans')}",{ op:'xiuzheng'},function(data){
						layer.msg(data.msg,{offset:'100px'});
						dialog(data.msg,data.status);
						layer.close(loadingdialog);
						tableIns.reload();
					})
				}
			}
		})
	}
	function getopenids(){
		layer.msg('正在获取第'+(getopenidnum*10000+1)+'~'+(((getopenidnum+1)*10000) <  totalfans ? ((getopenidnum+1)*10000) : totalfans)+'条openid',{offset:'100px'});
		$.post("{:url('tbfans')}",{ op:'getopenid','next_openid':next_openid},function(data){
			if(data.status==0){
				dialog(data.msg,data.status);return;
			}
			next_openid = data.next_openid;
			openids = data.openids;
			getuserinfo();

		},'json');
	}
	{if $count==0}tongbu();{/if}

	function settag(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}

		var html = '<div style="margin:20px auto;">';
		html+='<div class="layui-form" lay-filter="">';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:80px">选择标签：</label>';
		html+='		<div class="layui-input-inline" style="width:400px">';
		{foreach $tagslist as $tag}
		html+='			<input type="checkbox" name="tagids[]" value="{$tag.tagid}" title="{$tag.name}">';
		{/foreach}
		html+='		</div>';
		html+='	</div>';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:80px"></label>';
		html+='		<div class="layui-input-inline">';
		html+='			<button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit_settag">确 定</button>';
		html+='		</div>';
		html+='	</div>';
		html+='</div>';
		html+='</div>'
		var settaglayer = layer.open({type:1,area:['600px'],content:html,title:'设置标签',shadeClose:true});
		layui.form.render();
		layui.form.on('submit(submit_settag)', function(obj){
			var field = obj.field;
			field.fansids = ids;
			var index= layer.load();
			$.post("{:url('settag')}",field,function(data){
				layer.close(index);
				layer.close(settaglayer);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		})
	}
	</script>
	{include file="public/copyright"/}
</body>
</html>